<?php
/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
* A DHTML menu component for joomla/mambo
* @version 1.5.0
* @package ngmenu
* @copyright (C) 2004 - 2012 by Georg Lorenz
* @license Released under the terms of the GNU General Public License
**/

define('_CURRENT_NGMENU_VERSION', 150);

function com_install(){

	$database =& JFactory::getDBO();

	$errors = array();
	?>
	<table width="100%" border="0" style="background:#FFFFFF;">
    <tr>
		<td style="text-align:center;">
			<img src="components/com_ngmenu/assets/images/logo48.png" />
		</td>
	</tr>
	<tr>
		<td style="text-align:center;">
			Installation Process:<br />

	<?php
	require_once( JPATH_ROOT .DS.'administrator'.DS.'components'.DS.'com_ngmenu'.DS.'assets'.DS.'includes'.DS.'install.com.inc.php' );
	
	$query = "DELETE FROM #__modules WHERE module='mod_ngmenu' AND title='NGMenu'";
	$database->setQuery($query);
	$database->query();
	
	if(!$row){
		echo '<font color="black"><b>No previous database version found. Trying a fresh database installation!</b></font><br />';
		$query = "INSERT INTO #__ngmenu_config VALUES ('1', '"._CURRENT_NGMENU_VERSION."')";
		$database->setQuery($query);
		$database->query();
		if($error = $database->getErrorMsg()){
			$errors[] = $error;
		}
	}elseif($row[0]->version < _CURRENT_NGMENU_VERSION){
		echo '<font color="black"><b>An older database found. Trying a database update!</b></font><br />';
//		echo '<p><font color="red">As the database structure has been changed it is needed to edit all the menu designs to set up the expand icons for main menu and sub menu items accordingly.</font></p>';
//		echo '<p><font color="red">As the unit for the font size has been changed from pt to px you should edit all your menu configurations.';
//		echo 'Please increase the font size as you need. For example: if the font size was set to 8pt before you should change it to 11px.</font></p>';
		if(!empty($updates)){
			foreach($updates as $version => $statements){
				if($version > $row[0]->version && !empty($statements)){
					foreach($statements as $statement){
						$database->setQuery($statement);
						$database->query();
						if($error = $database->getErrorMsg()){
							$errors[] = $error;
						}
					}
				}
			}
		}
		$query = "UPDATE #__ngmenu_config SET version = '"._CURRENT_NGMENU_VERSION."'"
			   . "\n WHERE id='1'";
		$database->setQuery($query);
		$database->query();
		if($error = $database->getErrorMsg()){
			$errors[] = $error;
		}
	}else{
		echo '<font color="black"><b>No database update needed. The database is already up to date!</b></font><br />';
	}

	if(empty($errors)){
		echo '<br /><font color="green"><b>Installation completed without any errors.</b></font><br /><br />';
	}else{
		echo '<br /><font color="red"><b>Database update failed with following database errors:</b></font><br /><br />';
		foreach($errors as $error){
			echo '<font color="red">'.$error.'</font><br />';
		}
	}
?>
		</td>
	</tr>
</table>
<?php
}
?>